import { expect } from 'chai';
import qsUtil from '../src/qsUtil.js';
import assert from 'assert';

describe('qsUtil', () => {
    const url = 'https://example.com/foo?bar=42&answer=baz&world=haha#hash';
    const urlWithoutHash = 'https://example.com/foo?bar=42&answer=baz&world=haha';
    const urlWithoutQuery = 'https://example.com/foo#hash';
    const urlWithoutQueryOrHash = 'https://exaple.com/foo';
    describe('qsUtil.del', () => {
        it('should return the same if del keys is empty', () => {
            assert.equal(qsUtil.del(url), url);
        });

        it('should return the same if del keys is empty array', () => {
            assert.equal(qsUtil.del(url, []), url);
        });

        it('should keep the question mark if del key is bar', () => {
            assert.equal(qsUtil.del(url, 'bar'), 'https://example.com/foo?answer=baz&world=haha#hash');
        });

        it('should ignore the question mark if delete all keys', () => {
            assert.equal(qsUtil.del(url, ['bar', 'answer', 'world']), 'https://example.com/foo#hash');
        });

        it('should ignore the question mark if delete all keys(without hash)', () => {
            assert.equal(qsUtil.del(urlWithoutHash, ['bar', 'answer', 'world']), 'https://example.com/foo');
        });

        it('should be the same if keys are not available', () => {
            assert.equal(qsUtil.del(urlWithoutQueryOrHash, ['bar', 'answer', 'world']), urlWithoutQueryOrHash);
        });
    });

    describe('qsUtil.update', () => {
        it('should return url with right params', function() {
            const url = 'http://example.com';
            const params = {
                foo: 41,
                bar: 42,
            };

            expect(qsUtil.update(url, params)).to.be.eq('http://example.com?foo=41&bar=42');
        });

        it('should return url(with params) with right params', function() {
            const url = 'http://example.com?from=1';
            const params = {
                foo: 41,
                bar: 42,
            };

            expect(qsUtil.update(url, params)).to.be.eq('http://example.com?from=1&foo=41&bar=42');
        });

        it('should return url(with hash) with right params', function() {
            const url = 'http://example.com#hash';
            const params = {
                foo: 41,
                bar: 42,
            };

            expect(qsUtil.update(url, params)).to.be.eq('http://example.com?foo=41&bar=42#hash');
        });

        it('should return mp=1 if config.override is false', function() {
            const url = 'http://example.com?mp=1';
            const params = {
                mp: 2,
                foo: 'baz'
            };
            const config = {
                override: false
            };
            expect(qsUtil.update(url, params, config)).to.be.eq('http://example.com?mp=1&foo=baz');
        });

        it('should filter undefined params', function() {
            const url = 'http://example.com';
            const params = {
                foo: undefined
            };
            expect(qsUtil.update(url, params)).to.be.eq('http://example.com');
        });
    });
})
